package br.fot.granola.oboe.graphics.backend;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import br.fot.granola.oboe.graphics.EmptyBackend;
import br.fot.granola.oboe.util.Vector;

public class VoxelBackend extends EmptyBackend 
{
	private class Voxel
	{
		public int key;
		public boolean hit;
		
		public Voxel( int key )
		{
			this.key = key;
		}
		
		public void reset()
		{
			this.hit = false;
		}
	}
	
	private Hashtable< Integer, Voxel> sparseVoxels;
	
	public VoxelBackend()
	{
		super();
		this.sparseVoxels = new Hashtable<Integer, VoxelBackend.Voxel>();
	}
	
	public void addVoxel( int x, int y, int z )
	{
		int key = getKey( x, y, z );
		if( ! sparseVoxels.containsKey( key )) sparseVoxels.put(key, new Voxel(key));
	}
	
	public Voxel getVoxel( int x, int y, int z )
	{
		return sparseVoxels.get( getKey(x, y, z) );
	}
	
	private int getKey(int x, int y, int z )
	{
		if( x<0 || y<0 || z<0 ) return 0x80000000;
		return x&0x3FF | (y&0x3FF)<<10 | (z&0x3FF)<<20;
	}
	
	private void getCoordFromKey( int key, Vector v )
	{
		v.x = key&0x3FF;
		v.y = (key>>10)&0x3FF;
		v.z = (key>>20)&0x3FF;
	}

	public void compile()
	{
		Iterator< Map.Entry<Integer, Voxel> > it = this.sparseVoxels.entrySet().iterator();
		
		while( it.hasNext() )
		{
			Map.Entry<Integer, Voxel> me = it.next();
			me.getValue().reset();
		}
	}
}
